import request from '@/util/axios';
import type { Res, TPage } from '../types';
import * as T from './assetIncome_type';

export enum ApiPath {
    LIST = '/api/asset/project/list', //获取资产项目列表
    ADD = '/api/asset/project/create', //新增
    CHECK = '/api/asset/project/detail', //详情
    EDIT = '/api/asset/project/edit', //编辑
    EDL = '/api/asset/project/delete', //删除
    RELAOBJ = '/api/asset/project/create_associations', //关联资源
    DICTDATA = '/api/system/sys_dict', //获取字典数据
    GETRELAOBJ = '/api/asset/project/associations' //获取已经关联资源
}

/**
 * @method 获取资产项目列表
 */
export function getList(page: TPage, filter?: any): Promise<Res<T.TResourceList>> {
    const { dateRange } = filter;
    const params: { [key: string]: any } = {
        ...{
            page: 1,
            page_size: 10
        },
        ...page,
        ...filter
    };
    if (dateRange && dateRange.length === 2) {
        params.start_time = `${dateRange[0]} `;
        params.end_time = `${dateRange[1]}`;
    }
    delete params.total;
    delete params.dateRange;

    const paramArr: string[] = [];
    for (const key in params) {
        if (!params[key]) continue;
        paramArr.push(`${key}=${encodeURIComponent(params[key])}`);
    }
    return request.get(`${ApiPath.LIST}?${paramArr.join('&')}`);
}

export const projectList_get = (params: any) => request.get(ApiPath.LIST, { params });
/**
 * @method 资产项目新增
 */
export const add = (params: any) => request.post(ApiPath.ADD, params);

/**
 * @method 资产项目编辑
 */
export const edit = (params: any) => request.post(ApiPath.EDIT, params);

/**
 * @method 资产项目查看
 */
export const check = (params: any) => request.get(ApiPath.CHECK, { params });
export const projectDetail_get = (params: any) => request.get(ApiPath.CHECK, { params });
/**
 * @method 资产项目删除
 */
export const del = (params: any) => request.get(ApiPath.EDL, { params });

/**
 * @method 资产项目关联资源
 */
export const relatedObjects = (params: any) => request.post(ApiPath.RELAOBJ, params);

/**
 * @method 资产项目关联资源
 */
export const getDictSelect = (params: any) => request.get(ApiPath.DICTDATA, { params });

/**
 * @method 资产项目已经关联资源
 */
export const getRelatedObjects = (params: any) => request.get(ApiPath.GETRELAOBJ, { params });
